﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hrunga.PVCM.Framework.Controls;
using Hrunga.PVCM.Framework.EventArguments;

namespace Hrunga.PVCM.Web.Controls
{
    /// <summary>
    /// Custom implementation of a TextBox control
    /// </summary>
    public partial class TextBox : System.Web.UI.WebControls.TextBox, ITextBox
    {
        private const string OldValueAttribute = "originalValue";

        public TextBox()
        {
            base.TextChanged += OnBaseTextChanged;
        }
        
        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get { return this.ID; }
            set { this.ID = value; }
        }

        /// <summary>
        /// Gets the text associated with this control.
        /// </summary>
        /// <value></value>
        /// <returns>The text associated with this control.</returns>
        public new string Text
        {
            get { return base.Text; }
            set
            {
                this.Attributes[OldValueAttribute] = value;
                base.Text = value;
            }
        }
        
        /// <summary>
        /// Gets or sets a value indicating whether this instance is password.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is password; otherwise, <c>false</c>.
        /// </value>
        public bool IsPassword
        {
            get { return this.IsPassword; }
            set { this.IsPassword = value; }
        }

        /// <summary>
        /// Occurs when the text changes.
        /// </summary>
        public new event EventHandler<TextChangedEventArgs> TextChanged;

        private void OnBaseTextChanged(object sender, EventArgs eventArgs)
        {
            if (this.TextChanged != null)
            {
                var oldValue = this.Attributes[OldValueAttribute];
                var newValue = this.Text;
                var args = new TextChangedEventArgs(oldValue, newValue);
                this.TextChanged(this, args);
            }
        }
    }
}
